package com.cat.twoPointerTechnique;
/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/watering-plants-ii/
 * @create 2025/8/1 16:12
 * @since JDK17
 */

public class Solution03 {
    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int ans = 0, n = plants.length, a = capacityA, b = capacityB, l, r;
        for (l = 0, r = n - 1; l < r; ) {
            if (a >= plants[l]) {
                a -= plants[l++];
            } else {    // 装一次水
                a = capacityA - plants[l++];
                ans++;
            }
            if (b >= plants[r]) {
                b -= plants[r--];
            } else {
                b = capacityB - plants[r--];
                ans++;
            }
        }
        if (l == r) {
            ans += (Math.max(a, b) >= plants[l] ? 0 : 1);
        }
        return ans;
    }
}
